<?php
$access_level = 1;
$script ='survey';
require_once '../../controller.php';
require_once '../../classes/Survey.php';
require_once '../../classes/Question.php'; 

if(isset($_SESSION['logged_in'])){
	$instructor_id = $_SESSION['logged_in'];
} 

if(isset($_REQUEST['sid'])){
 $survey_id = $_REQUEST['sid'];
}

if(isset($_POST['set_condition'])){
	
	$question1_id = $_POST['question_1'];
	$question2_id = $_POST['question_2'];
	$condition = $_POST['condition'];
	$condition_value = $_POST['condition_value'];
	
	$sql = "INSERT INTO `branching` (`survey_id`,`question_1_id`,`question_2_id`,`condition`,`value`)
	                     VALUES ('$survey_id','$question1_id','$question2_id','$condition','$condition_value')";
	$result = mysql_query($sql) or die("Insert Condition:".mysql_error());
	
	if($result){
		header("location:branching.php?sid=$survey_id");
	}
	
}
require_once '../../header.php';
$s = new Survey();
$questions = $s->getQuestions($survey_id);
if($questions){
	$sec = 0;
	foreach($questions as $i=>$value){
		if($i !=0 && $value->create_section =='Y'){
			$sec++;
		}
		$question_data['id'] = $value->id; 
		$question_data['question']= $value->question;
		$question_data['type']=$value->type;
		$section[$sec][$i]= $question_data;
	}
	//die(print_r($section));
	echo "Set Conditions Here<br><br>";
	echo "Display Question This <br>";
	echo "<form name='condition_form' method='post'>";
	echo "<select name='question_1' size='3' >";
	foreach($section as $i=>$values){
	foreach($values as $key=>$value){	
	if($i>0 && $value['type'] !='Section Text'){
	echo "<option onclick='javascript: showSection($i);' value='".$value['id']."'>".$value['question']."</option>";	
	}	
	}
}
	echo "</select><br><br>";
	
	echo "IF Answer to Question This <br>";
	
	echo "<select name='question_2' size='3' >";
	foreach($questions as $i=>$value){
		$id = $value->id;
		$type = $value->type;
		$required = $value->required;
		$question = $value->question;
		$create_section = $value->create_section;
	
	if($type !='Section Text'){
	echo "<option value='$id'>$question</option>";	
	}	
	}
	echo "</select><br><br>";
	echo "IS <br>";
	echo "<select name='condition' size='3' >";
	echo "<option value='equal' >Equal To</option><option value='less' >Less Then</option><option value='greater' >Greater Then</option>";
	echo "</select>";	
	echo "&nbsp;&nbsp;&nbsp;"	;
	echo "<input type='text' name='condition_value'><br><br>";
	echo "<input type='submit' name='set_condition' value='Set Condition'> ";
	echo "</form><br><br>";
	
}

$s = new Survey();
$q = new Question();
$conditions = $s->getAllConditions($survey_id);
if($conditions){
foreach($conditions as $c=>$values){
	echo  "<font color='black' size ='5em'>Condition number. ".($c+1)."</font><br>";
	echo  "<font color='black' size ='5em'>Display Question:  </font>";
	foreach($values as $key=>$value){
	if($key == 'question_1_id'){
		$question_1_id = $value;	
	    }
	if($key == 'question_2_id'){
		$question_2_id = $value;	
	    }	
	if($key == 'condition'){
		$condition = $value;	
	    }
	if($key == 'condition_value'){
		$condition_value = $value;	
	    }
	}
	$question1 = $q->getQuestion($question_1_id);
	echo $question1->question."<br>";
	echo "<font color='black' size ='5em'>IF Answer to Question: </font>";
	$question2 = $q->getQuestion($question_2_id);
	echo $question2->question."<br>";
	echo "IS ";
	if($condition =='equal'){
		echo "<font color='black' size ='5em'>Equal To</font>";
	}
    if($condition =='less'){
		echo "<font color='black' size ='5em'>Less Then</font>";
	}
    if($condition =='greater'){
		echo "<font color='black' size ='5em'>Greater Then</font>";
	}
	
	echo "&nbsp;&nbsp;&nbsp;&nbsp;";
	echo $condition_value;
	echo "<br>--------------------------------------------------------------------------<br><br>";
	
}
}

require_once '../../footer.php';
?>